package daosimpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import model.Evaluacion;
import model.Permiso;
import model.Pregunta;
import model.Tematica;

import org.hibernate.HibernateException;




import org.hibernate.Query;
import org.hibernate.Transaction;

import daos.EvaluacionDAO;
import util.HibernateUtil;

public class EvaluacionDAOImpIH implements EvaluacionDAO  {
	
	private Transaction tx;
	
public Evaluacion insertCE(Evaluacion p)  throws HibernateException{

		try {
			
			tx = HibernateUtil.session.beginTransaction();
			HibernateUtil.session.save(p);
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}
			return p;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return null;        	
	}



public int obtenerId() throws HibernateException {
	int id = 0;
	List<Evaluacion> result = new ArrayList<Evaluacion>();
	try {
		
		tx = HibernateUtil.session.beginTransaction();
					
		Query query = HibernateUtil.session
				.createQuery("FROM Evaluacion");
		result = query.list();
		
		id=query.list().size();
	
		tx.commit();
		if (!tx.wasCommitted()){
		    tx.commit();
		}			
		
		
	} catch (Exception he) {
		System.out.println("mall " + he.getMessage() + he.getCause());
		tx.rollback(); 
	} finally { 
        //session.close(); 
    }  
	return id;
}


public List<Evaluacion> ListarEvaluaciones(String idgpc) throws HibernateException {
	List<Evaluacion> result = new ArrayList<Evaluacion>();
	
	try {
		
		tx = HibernateUtil.session.beginTransaction();
					
		Query query = HibernateUtil.session
				.createQuery("FROM Evaluacion where cursoPeridoGrupo.cursoPeridoGrupoId='"+
		idgpc +"'");
		result = query.list();
		
		if (query.list().isEmpty()) {
			System.out.println("esta vacio");
			System.out.println("esta vaciojscjdjdsjsdgkjsdgjw");
			return null;
		}
		tx.commit();
		if (!tx.wasCommitted()){
		    tx.commit();
		}			
		return result;
		
	} catch (Exception he) {
		System.out.println("mall " + he.getMessage() + he.getCause());
		tx.rollback(); 
	} finally { 
        //session.close(); 
    }  
	return result; 
}
public Evaluacion ce_examen(String idgpc)throws HibernateException{
	Evaluacion result = new Evaluacion();
	try {
		Date hoy=new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		tx = HibernateUtil.session.beginTransaction();
					
		Query query = HibernateUtil.session
				.createQuery("FROM Evaluacion where fechaProgramada = '"+
		format.format(hoy) +"'  and cursoPeridoGrupo.cursoPeridoGrupoId= '" +
		idgpc +"'");
		if(query.list().size()==0){ return null;}else{result = (Evaluacion) query.list().get(0);}
		result = (Evaluacion) query.list().get(0);
		System.out.println(query.list());
		System.out.println(query.list().size() +"holi");
		
		if (query.list().isEmpty()) {
			System.out.println("esta vacio");
			
		}
		tx.commit();
		if (!tx.wasCommitted()){
		    tx.commit();
		}			
		return result;
		
	} catch (Exception he) {
		System.out.println("mall " + he.getMessage() + he.getCause());
		tx.rollback(); 
	} finally { 
        //session.close(); 
    }  
	return result; 
}
public void  update(Evaluacion p) throws HibernateException{
	
	
	
	try {
		
		tx = HibernateUtil.session.beginTransaction();
					
		 HibernateUtil.session
			.update(p);
		tx.commit();
		
	} catch (Exception he) {
		System.out.println("mall " + he.getMessage() + he.getCause());
		tx.rollback(); 
	} finally { 
        //session.close(); 
    }  
	
}
public Evaluacion obtenerCE(String id)throws HibernateException{
	Evaluacion result = new Evaluacion();
	try {
		
		tx = HibernateUtil.session.beginTransaction();
					
		Query query = HibernateUtil.session
				.createQuery("FROM Evaluacion where evaluacion_id = '"+
		id +"'");
		result = (Evaluacion) query.list().get(0);
		System.out.println(query.list());
		System.out.println(query.list().size());
		if (query.list().isEmpty()) {
			System.out.println("esta vacio");
		}
		tx.commit();
		if (!tx.wasCommitted()){
		    tx.commit();
		}			
		return result;
		
	} catch (Exception he) {
		System.out.println("mall " + he.getMessage() + he.getCause());
		tx.rollback(); 
	} finally { 
        //session.close(); 
    }  
	return result; 
	
	
	
}

public int obtenerCantEvalxProfesor(String id) throws HibernateException {
	List<Evaluacion> result = new ArrayList<Evaluacion>();
	try {

		tx = HibernateUtil.session.beginTransaction();

		Query query = HibernateUtil.session
				.createQuery("FROM Evaluacion where cursoPeridoGrupo.profesor.docenteId = '" + id
						+ "'");
		result =  query.list();
		System.out.println(query.list());
		System.out.println(query.list().size());
		if (query.list().isEmpty()) {
			System.out.println("esta vacio");
		}
		tx.commit();
		if (!tx.wasCommitted()) {
			tx.commit();
		}
		return result.size();

	} catch (Exception he) {
		System.out.println("mall " + he.getMessage() + he.getCause());
		tx.rollback();
		return 0;
	} finally {
		// session.close();
	}
}

}


